﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Anke.SHManage.BLL;
using Anke.SHManage.Model;
using Anke.SHManage.Model.ViewModel;
using Anke.SHManage.Utility;
using Anke.SHManage.Model.ExtentionModel;
using System.Web.Script.Serialization;
using System.Configuration;
using System.Net;
using System.Text;

namespace Anke.SHManage.Web.Areas.PR.Controllers
{
    public class ChargeController : Controller
    {
        private JavaScriptSerializer js = new JavaScriptSerializer();
        //
        // GET: /PR/Charge/

        public ActionResult Index()
        {
            return View();
        }

        #region 判断登录人是否有超级权限 2016-7-13 zch
        public bool SuperRole(string Roles)
        {
            string strSuperRole = AppConfig.GetConfigString(Roles);
            string[] tcr = strSuperRole.Split(',');
            bool tarray = false;
            for (int i = 0; i < tcr.Length; i++)
            {
                bool UserRole = UserOperateContext.Current.Session_UsrRole.SingleOrDefault(p => p == Convert.ToInt32(tcr[i])) != 0 ? true : false;
                if (UserRole)
                {
                    //判断登录人角色
                    tarray = true;
                    break;
                }
            }
            return tarray;
        }
        #endregion

        public ActionResult PatinetCharge(string TaskCode, int PatientOrder, string AlarmeventType, string ChargeType, string SubmitLogo, string MedicalStateCode, string IsYB, string IsYL, string IsGJM)
        {
            ViewBag.TaskCode = TaskCode;
            ViewBag.PatientOrder = PatientOrder;
            ViewBag.AlarmeventType = AlarmeventType;
            ViewBag.ChargeType = ChargeType;
            ViewBag.SubmitLogo = SubmitLogo;
            ViewBag.MedicalStateCode = MedicalStateCode;//20170112--增加病历状态编码
            ViewBag.IsYB = IsYB; //20211018 plq--医保上线移植，增加是否医保
            ViewBag.IsYL = IsYL; //20220119 plq--银联电子支付上线移植，增加是否银联收费
            ViewBag.IsGJM = IsGJM; //20220328 plq--医保国家码上线移植，增加是否医保国家码
            //2016-7-8 zch 
            bool Insert = new UserOperateContext().getGongneng(1312);////判断登录人有无财务重打发票权限
            if (Insert)
            {
                this.ViewData["RolePermission"] = "Insert";//登录人有财务重打发票权限
            }
            else
            {
                this.ViewData["RolePermission"] = "";
            }
            //2016-7-13 zch  
            if (SuperRole("SuperRole"))
            {
                this.ViewData["SuperRole"] = "SuperRole";//登录人有超级权限
            }
            else
                this.ViewData["SuperRole"] = "";

            //2018-05-04 zch 根据事件类型来获取收费金额计算方式
            M_PatientChargeBLL bll = new M_PatientChargeBLL();
            M_PatientChargeDicInfo cdinfo = null;
            if (AlarmeventType == "回家") //非急救  
            { cdinfo = bll.GetChargeDictionary("1"); }
            else//急救
            { cdinfo = bll.GetChargeDictionary("2"); }

            ViewBag.PriceOfKM = cdinfo.PriceOfKM;
            ViewBag.OriginKM = cdinfo.OriginKM;
            ViewBag.LeastPrice = cdinfo.LeastPrice;
            ViewBag.FirstAidPrice = cdinfo.FirstAidPrice;

            return View();
        }
        [HttpPost]
        public ActionResult GetPatientCharge()
        {
            string taskCode = Request.Form["TaskCode"].ToString();
            string patientOrder = Request.Form["PatientOrder"].ToString();
            // return Json(list, "appliction/json", JsonRequestBehavior.AllowGet);
            List<PatientChargeInfo> listYP = null;
            List<PatientChargeInfo> listCL = null;
            List<PatientChargeInfo> listJC = null;
            List<PatientChargeInfo> listZL = null;
            dynamic info = null;
            dynamic editinfo = null;
            new M_PatientChargeBLL().getPatientCharge(taskCode, patientOrder, out listYP, out listCL, out listJC, out listZL, out info, out editinfo);
            return Json(new { PRInfo = info, EditPRInfo = editinfo, listYP = listYP, listCL = listCL, listJC = listJC, listZL = listZL }, "appliction/json", JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 病历收费新增(未收时保存提交)
        /// 2021-10-18 plq 医保上线移植改造
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SavePatientCharge(M_PatientCharge info)
        {
            //int userId = UserOperateContext.Current.Session_UsrInfo.ID; //自动出库 add2021-10-19 plq 医保上线移植
            int userId = new M_PatientRecordBLL().GetUserID(info.TaskCode, info.PatientOrder); //2021-12-24 plq 移植参考闵行，UserID赋值调整
            string errorMsg = "";
            bool retDelivery = SaveTaskDelivery(false, info.TaskCode, info.PatientOrder, userId, ref errorMsg);
            if (!retDelivery)
            {
                return Json(new { Result = "Error", Message = "自动出库失败！" + errorMsg }, "text/plain;charset=UTF-8", JsonRequestBehavior.AllowGet);
            }

            info.ChargePerson = UserOperateContext.Current.Session_UsrInfo.Name;
            info.IFArrearage = false;
            info.ChargeType = -1;//20210628gdg--修改为-1

            //2018-12-11 zch
            if (info != null && info.InvoiceNumber != null)
            {
                new I_InvoiceApprovalBLL().DealUsedInvoiceRecord(info.InvoiceNumber);
            }

            //20210527add收费子表--gdg //add2021-10-18 plq 医保上线移植
            string OrderNumber = DateTime.Now.ToString("yyyyMMddHHmmssfff") + UserOperateContext.Current.Session_UsrInfo.WorkCode;//订单号:时间拼上工号 
            M_PatientChargeDetail minfo = new M_PatientChargeDetail();
            minfo.TaskCode = info.TaskCode;
            minfo.PatientOrder = info.PatientOrder;
            minfo.ChargeOrder = 1;
            minfo.ChargeDate = info.Date;
            if (info.PaidMoney.ToString().IndexOf(".") > -1)
            {
                minfo.PaidMoney = Math.Floor(Convert.ToDouble(info.PaidMoney));//取整
            }
            else
            {
                minfo.PaidMoney = info.PaidMoney;
            }
            minfo.ChargeType = 0; //现金
            minfo.OrderNumber = OrderNumber;//20211012订单号
            minfo.IFRefund = false;
            minfo.IFAccountCheck = null;

            int mpcdxj = new M_PatientChargeDetailBLL().Add(minfo);//20210623新增

            M_PatientChargeDetail minfos = new M_PatientChargeDetail();
            int ress = 0;
            //20210623gdg--判断是否有小数--如果有增加分币误差类型7
            if (info.PaidMoney.ToString().IndexOf(".") > -1)
            {
                minfos.TaskCode = info.TaskCode;
                minfos.PatientOrder = info.PatientOrder;
                minfos.ChargeOrder = 2;
                minfos.ChargeDate = info.Date;
                string paidMoney = info.PaidMoney.ToString();
                minfos.PaidMoney = Convert.ToDouble("0" + paidMoney.Substring(paidMoney.IndexOf(".")));//小数部分
                minfos.ChargeType = 7; //分币误差
                minfos.OrderNumber = OrderNumber;//20210617订单号
                minfos.IFRefund = false;
                minfos.IFAccountCheck = null;
                ress = new M_PatientChargeDetailBLL().Add(minfos);
            }

            ////add2021-06-07 plq web端新增保存病历收费时也要同步保存到收费日志Log表 start---暂时不启用
            //M_PatientChargeLog pchargeLog = GetChargeLogInfo(info);
            //pchargeLog.Isvalid = true; //未收费病历的新增收费 Isvalid应该为true
            //int resMpcl = new M_PatientChargeLogBLL().Add(pchargeLog);
            ////同步保存到收费日志Log表 end

            if (new M_PatientChargeBLL().Add(info) > 0 && mpcdxj > 0)
            {
                return Json(new { Result = "OK", Message = "收费信息记录成功！" }, "text/plain;charset=UTF-8", JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { Result = "Error", Message = "收费信息记录失败！" }, "text/plain;charset=UTF-8", JsonRequestBehavior.AllowGet);
            }

        }

        ////病历收费修改
        //[HttpPost]
        //public ActionResult EditPatientCharge(M_PatientCharge info)
        //{
        //    info.ChargePerson = UserOperateContext.Current.Session_UsrInfo.Name;
        //    info.IFArrearage = false;
        //    //int res = new M_PatientChargeBLL().Modify(info, "Date", "InvoiceNumber", "PatientName", "AddressStart", "AddressEnd", "OutStationRoadCode", "PointRoadCode", "ArriveHospitalRoadCode", "ReturnStationRoadCode", "OneWayKM",
        //    //    "ChargeKM", "CarFee", "WaitingFee", "EmergencyFee", "DrugFeeTotal", "ExamineFeeTotal", "ConsumableFeeTotal", "MeasureFeeTotal", "ReceivableTotal", "PaidMoney", "ChargePerson", "IFArrearage", "ReceivablesPaidInconsistentReasons", "BackUp1", "baidufei", "TaskSD");
        //    //if (res > 0)
        //    //    return Json(new { Result = "OK", Message = "收费信息记录成功！" }, "text/plain;charset=UTF-8", JsonRequestBehavior.AllowGet);

        //    //else
        //    //    return Json(new { Result = "Error", Message = "收费信息记录失败！" }, "text/plain;charset=UTF-8", JsonRequestBehavior.AllowGet);


        //    //2018-12-11 zch
        //    if (info != null && info.InvoiceNumber != null)
        //    {
        //        new I_InvoiceApprovalBLL().DealUsedInvoiceRecord(info.InvoiceNumber);
        //    }

        //    if (info.IMEI != "PAD")   //2017-10-27 周志猛  PAD收费之后,不能更改收费信息
        //    {
        //        //int res = new M_PatientChargeBLL().Modify(info, "Date", "InvoiceNumber", "PatientName", "AddressStart", "AddressEnd", "OutStationRoadCode", "PointRoadCode", "ArriveHospitalRoadCode", "ReturnStationRoadCode", "OneWayKM",
        //        //    "ChargeKM", "CarFee", "WaitingFee", "EmergencyFee", "DrugFeeTotal", "ExamineFeeTotal", "ConsumableFeeTotal", "MeasureFeeTotal", "ReceivableTotal", "PaidMoney", "ChargePerson", "IFArrearage", "ReceivablesPaidInconsistentReasons", "BackUp1", "baidufei", "TaskSD");
        //        int res = new M_PatientChargeBLL().Modify(info, "Date", "InvoiceNumber", "PatientName", "AddressStart", "AddressEnd", "OutStationRoadCode", "PointRoadCode", "ArriveHospitalRoadCode", "ReturnStationRoadCode", "OneWayKM",
        //            "ChargeKM", "CarFee", "WaitingFee", "EmergencyFee", "DrugFeeTotal", "ExamineFeeTotal", "ConsumableFeeTotal", "MeasureFeeTotal", "ReceivableTotal", "PaidMoney", "ChargePerson", "IFArrearage", "ReceivablesPaidInconsistentReasons", "BackUp1", "baidufei", "PaidCarFee", "PaidPRFee");
        //        if (res > 0)
        //            return Json(new { Result = "OK", Message = "收费信息记录成功！" }, "text/plain;charset=UTF-8", JsonRequestBehavior.AllowGet);

        //        else
        //            return Json(new { Result = "Error", Message = "收费信息记录失败！" }, "text/plain;charset=UTF-8", JsonRequestBehavior.AllowGet);
        //    }
        //    else if (info.IMEI == "PAD" && info.BackUp1 != null)
        //    {
        //        //int ress = new M_PatientChargeBLL().Modify(info, "BackUp1", "ReceivablesPaidInconsistentReasons");
        //        int ress = new M_PatientChargeBLL().Modify(info, "BackUp1", "ReceivablesPaidInconsistentReasons", "PaidMoney", "PaidCarFee", "PaidPRFee", "InvoiceNumber", "Date");
        //        if (ress > 0)
        //            return Json(new { Result = "OK", Message = "收费信息记录成功！" }, "text/plain;charset=UTF-8", JsonRequestBehavior.AllowGet);

        //        else
        //            return Json(new { Result = "Error", Message = "收费信息记录失败！" }, "text/plain;charset=UTF-8", JsonRequestBehavior.AllowGet);
        //    }
        //    else
        //    {
        //        return Json(new { Result = "Error", Message = "PAD已收费,无法更改收费信息！" }, "text/plain;charset=UTF-8", JsonRequestBehavior.AllowGet);
        //    }
        //}


        //病历收费修改保存 //add2021-10-18 plq 医保上线移植(因为后端有是否PAD提交收费的条件判断，与闵行全部放开不同，这里参考上海市120FA源码进行的移植)
        [HttpPost]
        public ActionResult EditPatientCharge(M_PatientCharge info)
        {
            //int userId = UserOperateContext.Current.Session_UsrInfo.ID; //自动出库 add2021-10-19 plq 医保上线移植
            int userId = new M_PatientRecordBLL().GetUserID(info.TaskCode, info.PatientOrder); //2021-12-24 plq 移植参考闵行，UserID赋值调整
            string errorMsg = "";
            bool retDelivery = SaveTaskDelivery(false, info.TaskCode, info.PatientOrder, userId, ref errorMsg);
            if (!retDelivery)
            {
                return Json(new { Result = "Error", Message = "自动出库失败！" + errorMsg }, "text/plain;charset=UTF-8", JsonRequestBehavior.AllowGet);
            }

            info.ChargePerson = UserOperateContext.Current.Session_UsrInfo.Name;
            info.IFArrearage = false;
            info.ChargeType = -1;//20210628gdg--修改为-1

            //2018-12-11 zch
            if (info != null && info.InvoiceNumber != null)
            {
                new I_InvoiceApprovalBLL().DealUsedInvoiceRecord(info.InvoiceNumber);

            }

            //20210527add收费子表--gdg
            string OrderNumber = DateTime.Now.ToString("yyyyMMddHHmmssfff") + UserOperateContext.Current.Session_UsrInfo.WorkCode;//订单号:时间拼上工号 
            M_PatientChargeDetail minfo = new M_PatientChargeDetail();
            minfo.TaskCode = info.TaskCode;
            minfo.PatientOrder = info.PatientOrder;
            minfo.ChargeOrder = 1;
            minfo.ChargeDate = info.Date;
            if (info.PaidMoney.ToString().IndexOf(".") > -1)
            {
                minfo.PaidMoney = Math.Floor(Convert.ToDouble(info.PaidMoney));//20210623gdg取整
            }
            else
            {
                minfo.PaidMoney = info.PaidMoney;
            }
            minfo.ChargeType = 0; //现金
            minfo.OrderNumber = OrderNumber;//20210617订单号
            minfo.IFRefund = false;
            minfo.IFAccountCheck = null;

            if (info.IMEI != "PAD")   //2017-10-27 周志猛  PAD收费之后,不能更改收费信息
            {
                int res = new M_PatientChargeBLL().Modify(info, "Date", "InvoiceNumber", "PatientName", "AddressStart", "AddressEnd", "OutStationRoadCode", "PointRoadCode", "ArriveHospitalRoadCode", "ReturnStationRoadCode", "OneWayKM",
                    "ChargeKM", "CarFee", "WaitingFee", "EmergencyFee", "DrugFeeTotal", "ExamineFeeTotal", "ConsumableFeeTotal", "MeasureFeeTotal", "ReceivableTotal", "PaidMoney", "ChargePerson", "IFArrearage", "ReceivablesPaidInconsistentReasons", "BackUp1", "baidufei", "PaidCarFee", "PaidPRFee");
                //bool State = new M_PatientChargeBLL().UpdateChargeLog(info); //add2021-09-10 sr Web端保存病历收费时同步到Log表 //2021-10-19 plq 医保上线移植---暂时不启用

                int ress = 0;//20210623 //add2021-10-19 plq 医保上线移植(参考上海FA)
                int ressChargeDetail = 0;//20210623
                M_PatientChargeDetail model = new M_PatientChargeDetailBLL().GetModelWithOutTrace(a => a.TaskCode == info.TaskCode && a.PatientOrder == info.PatientOrder && a.ChargeOrder == 1);

                M_PatientChargeDetail model2 = new M_PatientChargeDetailBLL().GetModelWithOutTrace(b => b.TaskCode == info.TaskCode && b.PatientOrder == info.PatientOrder && b.ChargeOrder == 2);
                //20210623判断是否有分币误差数据
                if (model2 == null)
                {
                    ress = new M_PatientChargeDetailBLL().Modify(minfo, "ChargeDate", "PaidMoney", "ChargeType", "OrderNumber");//20210527add收费子表
                }
                else
                {
                    if (new M_PatientChargeDetailBLL().Del(model) > 0)
                    //删除收费子表数据M_PatientChargeDetail
                    {
                        ress = new M_PatientChargeDetailBLL().Add(minfo);//20210623新增

                        M_PatientChargeDetail minfos = new M_PatientChargeDetail();
                        //20210623gdg--判断是否有小数--如果有增加分币误差类型7
                        if (info.PaidMoney.ToString().IndexOf(".") > -1)
                        {
                            if (new M_PatientChargeDetailBLL().Del(model2) > 0)
                            {
                                minfos.TaskCode = info.TaskCode;
                                minfos.PatientOrder = info.PatientOrder;
                                minfos.ChargeOrder = 2;
                                minfos.ChargeDate = info.Date;
                                string paidMoney = info.PaidMoney.ToString();
                                minfos.PaidMoney = Convert.ToDouble("0" + paidMoney.Substring(paidMoney.IndexOf(".")));//小数部分
                                minfos.ChargeType = 7; //分币误差
                                minfos.OrderNumber = OrderNumber;//20210617订单号
                                minfos.IFRefund = false;
                                minfos.IFAccountCheck = null;
                                ressChargeDetail = new M_PatientChargeDetailBLL().Add(minfos);
                            }
                        }
                    }
                }

                //20210527如果收费主表和收费子表都修改成功--gdg
                if (res > 0 && ress > 0)
                {
                    return Json(new { Result = "OK", Message = "收费信息记录成功！" }, "text/plain;charset=UTF-8", JsonRequestBehavior.AllowGet);
                }
                else
                {
                    return Json(new { Result = "Error", Message = "收费信息记录失败！" }, "text/plain;charset=UTF-8", JsonRequestBehavior.AllowGet);
                }

            }
            else if (info.IMEI == "PAD" && info.BackUp1 != null)
            {
                //int ress = new M_PatientChargeBLL().Modify(info, "BackUp1", "ReceivablesPaidInconsistentReasons"); //add2021-10-19 plq 医保上线---PAD提交的收费金额在Web端不允许修改，只允许改补充说明和其他补充说明
                ////int ress = new M_PatientChargeBLL().Modify(info, "BackUp1", "ReceivablesPaidInconsistentReasons", "PaidMoney", "PaidCarFee", "PaidPRFee", "InvoiceNumber", "Date");
                //if (ress > 0)
                //    return Json(new { Result = "OK", Message = "收费信息记录成功！" }, "text/plain;charset=UTF-8", JsonRequestBehavior.AllowGet);

                //else
                //    return Json(new { Result = "Error", Message = "收费信息记录失败！" }, "text/plain;charset=UTF-8", JsonRequestBehavior.AllowGet);

                //web端放开对PAD端提交收费信息的修改---在无医保收费和银联收费的前提下 add2022-01-19 plq
                int ress = new M_PatientChargeBLL().Modify(info, "BackUp1", "ReceivablesPaidInconsistentReasons", "PaidMoney", "PaidCarFee", "PaidPRFee", "InvoiceNumber", "Date");
                //bool State = new M_PatientChargeBLL().UpdateChargeLog(info); //add2021-09-10 sr Web端保存病历收费时同步到Log表 //2021-10-19 plq 医保上线移植---暂时不启用
                
                //更新收费明细子表---先根据条件删除
                M_PatientChargeDetailBLL ubll = new M_PatientChargeDetailBLL();
                int delres = ubll.DelBy(p => p.TaskCode == info.TaskCode && p.PatientOrder == info.PatientOrder);
                if (delres >= 0) //add2022-02-07 plq 根据松江120现场PAD上传欠费、实收为0，明细表无数据删除操作返回0的特殊情况，增加=0条件
                {
                    int mpcdxj = new M_PatientChargeDetailBLL().Add(minfo);//20210623新增

                    M_PatientChargeDetail minfos = new M_PatientChargeDetail();
                    int mpcdxj2 = 0;
                    //20210623gdg--判断是否有小数--如果有增加分币误差类型7
                    if (info.PaidMoney.ToString().IndexOf(".") > -1)
                    {
                        minfos.TaskCode = info.TaskCode;
                        minfos.PatientOrder = info.PatientOrder;
                        minfos.ChargeOrder = 2;
                        minfos.ChargeDate = info.Date;
                        string paidMoney = info.PaidMoney.ToString();
                        minfos.PaidMoney = Convert.ToDouble("0" + paidMoney.Substring(paidMoney.IndexOf(".")));//小数部分
                        minfos.ChargeType = 7; //分币误差
                        minfos.OrderNumber = OrderNumber;//20210617订单号
                        minfos.IFRefund = false;
                        minfos.IFAccountCheck = null;
                        mpcdxj2 = new M_PatientChargeDetailBLL().Add(minfos);
                    }
                }

                if (ress > 0)
                    return Json(new { Result = "OK", Message = "收费信息记录成功！" }, "text/plain;charset=UTF-8", JsonRequestBehavior.AllowGet);

                else
                    return Json(new { Result = "Error", Message = "收费信息记录失败！" }, "text/plain;charset=UTF-8", JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { Result = "Error", Message = "PAD已收费,无法更改收费信息！" }, "text/plain;charset=UTF-8", JsonRequestBehavior.AllowGet);
            }
        }


        /// <summary>
        ///病历收费发票号修改保存 add2022-02-21 plq
        ///应松江120现场需求，在存在医保、银联收费时仅提供发票号修改
        ///不影响收费明细表M_PatientChargeDetail及银联记录表M_PatientPayRecord 
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult EditPatientChargeFPH(M_PatientCharge info)
        {
            int userId = new M_PatientRecordBLL().GetUserID(info.TaskCode, info.PatientOrder); //2021-12-24 plq 移植参考闵行，UserID赋值调整
            string errorMsg = "";
            bool retDelivery = SaveTaskDelivery(false, info.TaskCode, info.PatientOrder, userId, ref errorMsg);
            if (!retDelivery)
            {
                return Json(new { Result = "Error", Message = "自动出库失败！" + errorMsg }, "text/plain;charset=UTF-8", JsonRequestBehavior.AllowGet);
            }

            info.ChargePerson = UserOperateContext.Current.Session_UsrInfo.Name;
            info.IFArrearage = false;
            info.ChargeType = -1;//20210628gdg--修改为-1

            //2018-12-11 zch
            if (info != null && info.InvoiceNumber != null)
            {
                new I_InvoiceApprovalBLL().DealUsedInvoiceRecord(info.InvoiceNumber);

            }

            if (info.IMEI == "PAD")
            {
                int ress = new M_PatientChargeBLL().Modify(info, "InvoiceNumber"); //仅提供发票号修改，不影响收费明细表及银联记录表
                //bool State = new M_PatientChargeBLL().UpdateChargeLog(info); //add2021-09-10 sr Web端保存病历收费时同步到Log表 //2021-10-19 plq 医保上线移植---暂时不启用

                if (ress > 0)
                    return Json(new { Result = "OK", Message = "收费信息记录成功！" }, "text/plain;charset=UTF-8", JsonRequestBehavior.AllowGet);
                else
                    return Json(new { Result = "Error", Message = "收费信息记录失败！" }, "text/plain;charset=UTF-8", JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { Result = "Error", Message = "非PAD收费，不存在医保/银联收费明细类型！" }, "text/plain;charset=UTF-8", JsonRequestBehavior.AllowGet);
            }
        }

        /// <summary>
        /// 根据传来的收费实体转换收费日志实体
        /// add2021-10-12 zll 
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        private M_PatientChargeLog GetChargeLogInfo(M_PatientCharge charge)
        {
            M_PatientChargeLog pcharge = new M_PatientChargeLog();
            pcharge.ID = System.Guid.NewGuid();
            pcharge.TaskCode = charge.TaskCode;
            pcharge.PatientOrder = charge.PatientOrder;
            pcharge.Date = charge.Date;
            pcharge.InvoiceNumber = charge.InvoiceNumber;
            pcharge.PatientName = charge.PatientName;
            //pcharge.AddressStart = charge.AddressStart;
            //pcharge.AddressEnd = charge.AddressEnd;
            //pcharge.OutStationRoadCode = charge.OutStationRoadCode;
            pcharge.PointRoadCode = charge.PointRoadCode;
            pcharge.ArriveHospitalRoadCode = charge.ArriveHospitalRoadCode;
            //pcharge.ReturnStationRoadCode = charge.ReturnStationRoadCode;
            pcharge.OneWayKM = charge.ChargeKM;
            pcharge.ChargeKM = charge.ChargeKM;
            pcharge.CarFee = charge.CarFee;
            pcharge.WaitingFee = charge.WaitingFee;
            pcharge.EmergencyFee = charge.EmergencyFee;
            pcharge.DrugFeeTotal = charge.DrugFeeTotal;
            pcharge.ExamineFeeTotal = charge.ExamineFeeTotal;
            pcharge.ConsumableFeeTotal = charge.ConsumableFeeTotal;
            pcharge.MeasureFeeTotal = charge.MeasureFeeTotal;
            pcharge.ReceivableTotal = charge.ReceivableTotal;
            pcharge.PaidMoney = charge.PaidMoney;
            pcharge.ChargePerson = charge.ChargePerson;
            pcharge.IFArrearage = false;//是否欠费
            pcharge.TaskSD = charge.TaskSD;
            //pcharge.ReceivablesPaidInconsistentReasons = charge.ReceivablesPaidInconsistentReasons;
            //pcharge.BackUp1 = charge.BackUp1;
            pcharge.baidufei = charge.baidufei;
            //pcharge.IMEI = "PAD"; //add2021-06-07 plq Web端给未收费病历新增收费时没有IMEI号字段

            pcharge.PaidCarFee = charge.PaidCarFee; //2017-11-09 周志猛 添加车费实收
            pcharge.PaidPRFee = charge.PaidPRFee;//2017-11-09 周志猛 添加治疗费实收
            return pcharge;

        }

        #region 20210909 sr 上传收费扣库存接口 //2021-10-18 plq 医保上线移植
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public Boolean SaveTaskDelivery(bool isCheck, string taskCode, int patientOrder, int userId, ref string errorMsg)
        {
            try
            {
                string strURL = "";
                string responseText = "";
                strURL = ConfigurationManager.AppSettings["YBWebSaveTaskDelivery"].ToString();
                System.Net.HttpWebRequest request;
                request = (System.Net.HttpWebRequest)WebRequest.Create(strURL);
                //Post请求方式
                request.Method = "POST";
                // 内容类型
                request.ContentType = "application/x-www-form-urlencoded";
                // 参数经过URL编码
                StringBuilder sb = new StringBuilder();
                sb.Append("&isCheck=" + isCheck);
                sb.Append("&taskCode=" + taskCode);
                sb.Append("&patientOrder=" + patientOrder);
                sb.Append("&userId=" + userId);
                byte[] payload;
                //将URL编码后的字符串转化为字节
                payload = System.Text.Encoding.UTF8.GetBytes(sb.ToString());
                //设置请求的 ContentLength 
                request.ContentLength = payload.Length;
                //获得请求流
                System.IO.Stream writer = request.GetRequestStream();
                //将请求参数写入流
                writer.Write(payload, 0, payload.Length);
                // 关闭请求流
                writer.Close();
                System.Net.HttpWebResponse response;
                // 获得响应流
                response = (System.Net.HttpWebResponse)request.GetResponse();
                System.IO.StreamReader myreader = new System.IO.StreamReader(response.GetResponseStream(), Encoding.UTF8);
                responseText = myreader.ReadToEnd();
                myreader.Close();
                //解析返回的实体
                ResultInfo rinfo = js.Deserialize<ResultInfo>(responseText);
                if (rinfo.status == "0")
                {
                    return true;
                }
                else
                {
                    errorMsg = rinfo.msg;
                    return false;
                }
            }
            catch (Exception ex)
            {
                LogUtility.Error("PatientChargeController/SaveTaskDelivery", "程序异常：" + ex.Message);
                return false;
            }
        }
        #endregion

    }
}